/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package yassp.server;

import java.net.*;
import java.io.*;

/**
 *
 * @author pierre
 */
public class ListeningConnection {

    private static int port = 12345;
    private ServerSocket server = null;

    public ListeningConnection() {
        
        try {
            server = new ServerSocket(port); /* start listening on the port */
            System.out.println("Listening on port: " + port);
        } catch (IOException e) {
            System.out.println("Could not listen on port: " + port);
            System.err.println(e);
            System.exit(1);
        }
    }

    public void listen(){
        Socket client = null;
        while(true) {
            try {
                client = server.accept();
            } catch (IOException e) {
                System.err.println("Accept failed.");
                System.err.println(e);
                System.exit(1);
            }
            /* start a new thread to handle this client */
            System.out.println("New client connection: "+client.getRemoteSocketAddress());
            Thread t = new Thread(new ServerConnection(client));
            t.start();
        }
    }



}
